package com.shop.scheduled;

import com.shop.service.SeckillService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

//秒杀商品的定时上架：
//上架最近三天的商品
@Service
@Slf4j
public class SeckillSkuScheduled {
@Autowired
private  SeckillService seckillService;
@Autowired
private RedissonClient redissonClient;

private final String upload_lock="seckill:upload:lock";

//@Scheduled(cron = "20 * * * * ?")
    public void uploadSeckillSkuLatest3Days(){
    //重复上架无需处理
    log.info("上架秒杀的商品信息");

    RLock lock = redissonClient.getLock(upload_lock);//上锁
    lock.lock(60, TimeUnit.SECONDS);
    try {
        log.info("上锁");
        seckillService.uploadSeckillSkuLatest3Ddays();
    }finally {
        log.info("解锁");
        lock.unlock();//解锁
    }
}

}
